package com.bjsxt.chapter1_singleton;

import java.util.concurrent.CountDownLatch;

/**
 * @ClassName Client3
 * @Description 测试多线程环境下五种创建单例的方式的效率
 * @Author 黑马程序员
 * @Date 19:24 2024/6/19
 * @Version 1.0
 **/
public class Client3 {

    public static void main(String[] args) {
        int threadNum = 10;
        final CountDownLatch count = new CountDownLatch(threadNum);
        long start = System.currentTimeMillis();
        try {
            for (int i = 0; i < threadNum; i++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int j = 0; j < 1000000; j++) {
                            SingletonDemo5 s1 = SingletonDemo5.INSTANCE;
                        }
                        count.countDown();
                    }
                }).start();
            }
            // main线程阻塞，直到计数器变为0，才会继续往下执行！
            count.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();

        System.out.println("总耗时: " + (end - start));
        // 33ms     饿汉式
        // 364ms    懒汉式
        // 34ms     双重检测锁式
        // 58ms     静态内部类式
        // 46ms     枚举式
    }
}
